package com.aygxy.carproject.controller;

import com.aygxy.carproject.pojo.category.dto.CategoryAddNewDTO;
import com.aygxy.carproject.pojo.category.dto.CategoryUpdateInfoDTO;
import com.aygxy.carproject.pojo.category.entity.CategoryTreeEntity;
import com.aygxy.carproject.pojo.category.vo.CategoryStandardVO;
import com.aygxy.carproject.pojo.category.vo.CategoryTreeVO;
import com.aygxy.carproject.pojo.user.vo.UserListItemVO;
import com.aygxy.carproject.service.CategoryService;
import com.aygxy.carproject.service.CategoryTreeService;
import com.aygxy.carproject.web.JsonPage;
import com.aygxy.carproject.web.JsonResult;
import com.aygxy.carproject.web.WebConst;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "02. 类别管理模块")
@RestController
@Slf4j
@RequestMapping("/categories")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    CategoryTreeService categoryTreeService;

    /**
     * 新增类别
     */
    @ApiOperationSupport(order = 100)
    @ApiOperation(value = "新增类别")
    @PostMapping("/addnew")
    public JsonResult<Long> addNew( CategoryAddNewDTO categoryAddNewDTO) {
        Long id = categoryService.addNew(categoryAddNewDTO);
        return JsonResult.ok(id);
    }

    @ApiOperationSupport(order = 101)
    @ApiOperation("查询获得三级分类树对象")
    @GetMapping("/all")
    public JsonResult<CategoryTreeVO<CategoryTreeEntity>> getTreeVO(){
        CategoryTreeVO<CategoryTreeEntity> treeVO=
                categoryTreeService.categoryTree();
        return JsonResult.ok(treeVO);
    }
    /**
     * 删除类别
     */
    @ApiOperationSupport(order = 103)
    @ApiOperation(value = "删除类别")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别id", paramType = "path", required = true, dataType = "long" )
    })
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        categoryService.deleteById(id);
        return JsonResult.ok();
    }

    /**
     * 启用类别
     */
    @ApiOperationSupport(order = 201)
    @ApiOperation(value = "启用类别")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别id", paramType = "path", required = true, dataType = "long")
    })
    @PostMapping("/{id:[0-9]+}/status/enable")
    public JsonResult<Void> setEnableById(@PathVariable Long id) {
        categoryService.setEnableById(id);
        return JsonResult.ok();
    }

    /**
     * 禁用类别
     */
    @ApiOperationSupport(order = 202)
    @ApiOperation(value = "禁用类别")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别id", paramType = "path", required = true, dataType = "long")
    })
    @PostMapping("/{id:[0-9]+}/status/disable")
    public JsonResult<Void> setDisableById(@PathVariable Long id) {
        categoryService.setDisableById(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询类别列表")
    @ApiOperationSupport(order = 203)
    @GetMapping("/list")
    public JsonResult<List<CategoryStandardVO>> list() {
        log.debug("开始处理【查询用户列表】的请求……");
        List<CategoryStandardVO> list = categoryService.getCategoryList();
        return JsonResult.ok(list);
    }
    /**
     * 更新类别信息
     */
    @ApiOperationSupport(order = 204)
    @ApiOperation(value = "更新类别信息", notes = "需要具有【写】权限：car/admin/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别id", paramType = "path", required = true, dataType = "long")
    })
    @PreAuthorize("hasAuthority('car/admin/update')")
    @PostMapping("/{id:[0-9]+}/full-info/update")
    public JsonResult<Void> updateById(@PathVariable Long id, @Valid CategoryUpdateInfoDTO categoryUpdateInfoDTO) {
        categoryService.updateInfoById(id, categoryUpdateInfoDTO);
        return JsonResult.ok();
    }

    /**
     * 查询类别详情
     */
    @ApiOperationSupport(order = 205)
    @ApiOperation(value = "查询类别详情", notes = "需要管理员【读】权限：car/admin/read")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别id", paramType = "path", required = true, dataType = "long")
    })
    @PreAuthorize("hasAuthority('car/admin/read')")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<CategoryStandardVO> getById(@PathVariable Long id) {
        return JsonResult.ok(categoryService.getById(id));
    }

    /**
     * 根据父级类别查询子级类别列表
     */
    @ApiOperationSupport(order = 300)
    @ApiOperation(value = "根据父级类别查询子级类别列表", notes = "需要管理员【读】权限：car/admin/read")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父级类别id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int")
    })
    @PreAuthorize("hasAuthority('car/admin/read')")
    @GetMapping("/list-by-parent")
    public JsonResult<JsonPage<CategoryStandardVO>> listByParent(
            @RequestParam Long parentId,
            @RequestParam(required = false, defaultValue = WebConst.DEFAULT_PAGE) Integer page,
            @RequestParam(required = false, defaultValue = WebConst.DEFAULT_PAGE_SIZE) Integer pageSize) {
        JsonPage<CategoryStandardVO> categories = categoryService.listByParent(parentId, page, pageSize);
        return JsonResult.ok(categories);
    }


}
